package com.app.modules.web.pagetag;

import java.io.IOException;
import java.io.Writer;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

@SuppressWarnings("serial")
public class PageTagImageJpg extends TagSupport {
	private static final String FIRST_PAGE_KEY_HAVE      =   "<img src='images/page/jpg/first_page_have.jpg' border='0'>";
	private static final String FIRST_PAGE_KEY_HAVE_NOT  =   "<img src='images/page/jpg/first_page_no.jpg' border='0'>";
	private static final String PRE_PAGE_KEY_HAVE        =   "<img src='images/page/jpg/previous_page_have.jpg' border='0'>";
	private static final String PRE_PAGE_KEY_HAVE_NOT    =   "<img src='images/page/jpg/previous_page_no.jpg' border='0'>";
	
	private static final String NEXT_PAGE_KEY_HAVE       =   "<img src='images/page/jpg/next_page_have.jpg' border='0'>";
	private static final String NEXT_PAGE_KEY_HAVE_NOT   =   "<img src='images/page/jpg/next_page_no.jpg' border='0'>";
	private static final String LAST_PAGE_KEY_HAVE       =   "<img src='images/page/jpg/last_page_have.jpg' border='0'>";
	private static final String LAST_PAGE_KEY_HAVE_NOT   =   "<img src='images/page/jpg/last_page_no.jpg' border='0'>";
	
	private String formId = "document.forms[0]";

	public void setFormId(String formId) {
		this.formId = "document.getElementById('" + formId + "')";
	}
	
	public int doStartTag() throws JspException {
		// 取出Form里的Page对象.
		Page page = (Page) pageContext.findAttribute("page");

		// 打印翻页链接及翻页JavaScript.
		printNavigation(page);
		printTurnPageJavaScript();

		return EVAL_BODY_INCLUDE;
	}

	// 打印导航条.
	private void printNavigation(Page page) {
		StringBuffer html_body = new StringBuffer();
		
		if (page.isHasPre()) {
			html_body.append(getHref(1,FIRST_PAGE_KEY_HAVE));
			html_body.append("&nbsp;");
			html_body.append(getHref(page.getPrePage(),PRE_PAGE_KEY_HAVE));
		} else {
			html_body.append(FIRST_PAGE_KEY_HAVE_NOT);
			html_body.append("&nbsp;");
			html_body.append(PRE_PAGE_KEY_HAVE_NOT);
		}
		
		html_body.append("&nbsp;");
		html_body.append(getJumpControl(page));
		html_body.append("&nbsp;");
		
		if (page.isHasNext()) {
			html_body.append(getHref(page.getNextPage(),NEXT_PAGE_KEY_HAVE));
			html_body.append("&nbsp;");
			html_body.append(getHref(page.getTotalPages(),LAST_PAGE_KEY_HAVE));
		} else {
			html_body.append(NEXT_PAGE_KEY_HAVE_NOT);
			html_body.append("&nbsp;");
			html_body.append(LAST_PAGE_KEY_HAVE_NOT);
		}
		
		pageContextWrite(html_body.toString());
	}
	
	// 跳转选择框 .
	private String getJumpControl(Page page) {
		int turnPage = page.getPageNo();
		StringBuffer buf = new StringBuffer();
		buf.append("<select id=\"pageNo\" name=\"").append("page.pageNo\" onChange=\"javascript:jumpPage(this.value)\">");
		long totalPages = page.getTotalPages();
		for (int i = 1; i <= totalPages; i++) {
			buf.append("<option value='").append(i);
			// 如果翻页参数等于当前页则选择该选项.
			if (turnPage == i) {
				buf.append("' selected");
			}
			else buf.append("'");
			buf.append(">").append(i).append("/").append(totalPages).append("</option>");
		}
		buf.append("</select>");
		return buf.toString();
	}
	
	// 根据当前页获取链接字.
	private String getHref(long pageNo,String pageText) {
			StringBuffer buf = new StringBuffer();
			buf.append("<a href=\"#\"");
			buf.append(" onclick=\"jumpPage(").append(pageNo).append(");return false;\">");
			buf.append(pageText);
			buf.append("</a>");
			return buf.toString();
	}	

	// 打印隐藏翻页字段及翻页JavaScript.
	private void printTurnPageJavaScript() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<script type='text/javascript'>\n")
			  .append("function jumpPage(pageNo) {\n")
			  .append("var form = ").append(formId).append(";\n")
			  .append("if(pageNo == -1){\n")
			  .append("pageNo = form[\"pageNo\"].value;\n")
			  .append("var reg = /^([1-9][0-9]*)$/;\n")
			  .append("if(!reg.test(pageNo)){\n")
			  .append("alert(\"请输入正整数的页码 ！\");\n")
			  .append("return false;\n")
			  .append("}\n}\n")
			  .append("form[\"pageNo\"].value = pageNo;\n")
			  .append("form.submit();\n")
			  .append("}\n")
			  .append("</script>\n");
		
		pageContextWrite(buffer.toString());
	}
	
	// 打印.
	private void pageContextWrite(String html){
		try {
			Writer out = pageContext.getOut();
			out.write(html);
			out.flush();
		}
		catch (IOException e) {
			throw new RuntimeException("pageContext write.io exception");
		}
	}

}
